﻿using System;
using Validity.Contexts;
using Validity.Localization;
using Validity.Rules;

namespace Validity
{
  public static class IsGreaterThanOrEqualToConstraint
  {
    public static ComparableValidationContext<TObject, TValue> IsGreaterThanOrEqualTo<TObject, TValue>(this IValidationContext<TObject, TValue> context, TValue value)
      where TValue : IComparable
    {
      return context.IsGreaterThanOrEqualTo(obj => value);
    }

    public static ComparableValidationContext<TObject, TValue> IsGreaterThanOrEqualTo<TObject, TValue>(this IValidationContext<TObject, TValue> context, Func<TValue> valueRetriever)
      where TValue : IComparable
    {
      Verify.NotNull(valueRetriever);
      return context.IsGreaterThanOrEqualTo(obj => valueRetriever.Invoke());
    }

    public static ComparableValidationContext<TObject, TValue> IsGreaterThanOrEqualTo<TObject, TValue>(this IValidationContext<TObject, TValue> context, Func<TObject, TValue> valueRetriever)
      where TValue : IComparable
    {
      Verify.NotNull(valueRetriever);

      context.AddRule(new ComparisonRule<TObject, TValue>(valueRetriever, result => result >= 0, (obj, val) => String.Format(RuleViolations.GreaterThanOrEqualTo, valueRetriever.Invoke(obj))));

      return new ComparableValidationContext<TObject,TValue>(context);
    }
  }
}
